
$(document).ready(function() {
    // 获取选择框元素
    var languageSelect = document.getElementById('language');
    var selectedLanguage = ''; // 全局变量用于存储当前选择的语言

    // 监听选择框变化事件
    languageSelect.addEventListener('change', function() {
        selectedLanguage = languageSelect.value;
    });

    // 生成测试用例按钮点击事件
    $('#generate').click(function() {
        const code = $('#code_textarea').val()
        console.log(code)
        switch (selectedLanguage) {
            case 'python':
                console.log('save_code')
                 $.ajax({
                    url: "/save_code",
                    type: "post",
                    data: {
                        "code":code
                    },
                    success: function (res) {
                        console.log(res)
                        $('#test').val(res)
                    }
                })
                break;
            case 'java':
                console.log('save_codeJava')
                $.ajax({
                    url: "/save_codeJava",
                    type: "post",
                    data: {
                        "code":code
                    },
                    success: function (res) {
                        console.log(res)
                        $('#test').val(res)
                    }
                })
                break;
            case 'cpp':
                 console.log('save_code_cpp')
                $.ajax({
                    url: "/save_code_cpp",
                    type: "post",
                    data: {
                        "code":code
                    },
                    success: function (res) {
                        console.log(res)
                        $('#test').val(res)
                    }
                })
                break;
            case 'c':
                console.log('C语言');
                break;
            default:
                console.log('请选择语言');
                break;
        }
    })

    // 生成语法树按钮点击事件
    $('#generate_tree').click(function() {
        const code = $('#code_textarea').val()
        console.log(code)
        switch (selectedLanguage) {
            case 'python':
                 console.log('generate_tree')
                 $.ajax({
                    url: "/generate_tree",
                    type: "POST",
                    data: {
                        "code": code
                    },
                    success: function (res) {
                        console.log(res);
                        // 获取生成的图片 URL
                        const imageUrl = res.image_url;
                        // 在新窗口中打开图片
                        window.open(imageUrl, '_blank');
                    }
                })
                break;
            case 'java':
                console.log('generate_tree_java')
                $.ajax({
                    url: "/generate_tree_java",
                    type: "POST",
                    data: {
                        "code": code
                    },
                    success: function (res) {
                        console.log(res);
                        // 获取生成的图片 URL
                        const imageUrl = res.image_url;
                        // 在新窗口中打开图片
                        window.open(imageUrl, '_blank');
                    }
                })
                break;
            case 'cpp':
                console.log('generate_tree_cpp')
                $.ajax({
                    url: "/generate_tree_cpp",
                    type: "POST",
                    data: {
                        "code": code
                    },
                    success: function (res) {
                        console.log(res);
                        // 获取生成的图片 URL
                        const imageUrl = res.image_url;
                        // 在新窗口中打开图片
                        window.open(imageUrl, '_blank');
                    }
                })
                break;
            case 'c':
                console.log('C语言');
                break;
            default:
                console.log('请选择语言');
                break;
        }
    });

    // 下载测试用例
    $('#downloadCase').click(function () {
        const testCase = $('#test').val()
        console.log(testCase)
        $.ajax({
            url: "/downloadCase",
            type: "post",
            data: {
                "testCase":testCase
            },
            xhrFields: {
                responseType: 'blob' // 确保响应类型为 blob
            },
           success: function (data, textStatus, xhr) {
                // 创建一个链接元素用于下载
                var url = window.URL.createObjectURL(data);
                var a = document.createElement('a');
                a.href = url;
                a.download = 'test_case.txt';  // 设置下载文件名
                document.body.appendChild(a);
                a.click();
                window.URL.revokeObjectURL(url);  // 释放URL对象
                a.remove();  // 移除创建的链接元素
            },
        })
    })

});
